package com.administrator.scoreManageSys.controller.course;

import com.administrator.scoreManageSys.service.ExamMangeService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER;

/**
 * @author zsh
 * @date 2022/04/08/ 15:56
 */
@RestController
@RequestMapping(value = "/exam/manage")
public class ExamMangeController {

    @Resource
    private ExamMangeService examMangeService;

    /**
     * 获取考试管理列表
     *
     * @return 列表
     */
    @PostMapping(value = "/examList")
    @RequiresRoles("education")
    @RequiresPermissions(value = "examination:list", logical = Logical.OR)
    public JSONObject examList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            Long subject = data.getLong("subject");
            Long season = data.getLong("season");
            String name = data.getString("name");
            return examMangeService.examList(pageNum, pageSize,subject,season,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取考试管理列表 异常");
        }
    }

    /**
     * 添加考试
     *
     * @return 列表
     */
    @PostMapping(value = "/examAdd")
    @RequiresRoles("education")
    @RequiresPermissions(value = "examination:add", logical = Logical.OR)
    public JSONObject examAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            Long season = data.getLong("season");
            Long classId = data.getLong("classId");
            Long subject = data.getLong("subject");
            String name = data.getString("name");
            return examMangeService.examAdd(userId, season,classId,subject,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加考试 异常");
        }
    }


    /**
     * 修改考试
     *
     * @return 列表
     */
    @PostMapping(value = "/examEdit")
    @RequiresRoles("education")
    @RequiresPermissions(value = "examination:edit", logical = Logical.OR)
    public JSONObject examEdit(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            Long season = data.getLong("season");
            Long classId = data.getLong("classId");
            Long subject = data.getLong("subject");
            String name = data.getString("name");
            return examMangeService.examEdit(id, season,classId,subject,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改考试 异常");
        }
    }

    /**
     * 删除考试
     *
     * @return 列表
     */
    @PostMapping(value = "/examDelete")
    @RequiresRoles("education")
    @RequiresPermissions(value = "examination:add", logical = Logical.OR)
    public JSONObject examDelete(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return examMangeService.examDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除考试 异常");
        }
    }


}
